<?php

/**
 * Created
 * User: zuoa
 * Date: 2016/11/21 0021
 * Time: 19:35
 */
class User extends Y_Controller
{
    public function __construct()
    {
        parent::__construct();

    }

    public function checked()
    {
        $this->check_login();
        $search = $this->input->get("s");

        if ($this->is_post()) {
            $id = $this->input->post("id");
            $this->user_model->check_user($id, 1);
            $this->response(200, "success", null);
        } else {
            $users = $this->user_model->get_checked_users($search);
            $data['users'] = $users;
            if ($search != "") {
                $data['search'] = $search;
            }

            $this->load->view('dashboard/header', $this->_get_context());
            $this->load->view('dashboard/user/checked', $data);
            $this->load->view('dashboard/footer');
        }
    }

    public function uncheck()
    {
        $this->check_login();
        $search = $this->input->get("s");

        if ($this->is_post()) {
            $id = $this->input->post("id");
            $this->user_model->check_user($id, 0);
            $u = $this->user_model->get_user($id);
            $this->preset_model->deactive($u->idcard);
            $this->response(200, "success", null);
        } else {
            $users = $this->user_model->get_uncheck_users($search);
            $data['users'] = $users;
            if ($search != "") {
                $data['search'] = $search;
            }
            $this->load->view('dashboard/header', $this->_get_context());
            $this->load->view('dashboard/user/uncheck', $data);
            $this->load->view('dashboard/footer');
        }
    }

    public function manage()
    {
        $this->check_login();
        $search = $this->input->get("s");

        if ($this->is_post()) {
            $u = array(
                "name" => $this->input->post("name"),
                "dept" => $this->input->post("dept"),
                "idcard" => $this->input->post("idcard"),
                "pid" => $this->input->post("pid")
            );
            if ($this->user_model->add_preset_police($u)) {
                $this->response(200);
            } else {
                $error = $this->db->error();
                if (strpos($error["message"], 'PRIMARY') !== false) {
                    $this->response(201, "当前身份证已经存在", $error);
                } else if (strpos($error["message"], 'pid') !== false) {
                    $this->response(201, "当前警号已经存在", $error);
                } else {
                    $this->response(201, $error["message"], $error);
                }


            }


        } else {
            $users = $this->user_model->get_preset_users($search);
            $data['users'] = $users;
            if ($search != "") {
                $data['search'] = $search;
            }
            $this->load->view('dashboard/header', $this->_get_context());
            $this->load->view('dashboard/user/manage', $data);
            $this->load->view('dashboard/footer');
        }
    }



}